/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boot;

import boot.constant.BootPropertiesLoader;
import com.sun.syndication.feed.synd.SyndCategoryImpl;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndLinkImpl;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import persistence.facade.FacadePersistence;

/**
 *
 * @author Ricardo
 */
public class RssBoot {


    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getRootLogger();
    BootPropertiesLoader properties = new BootPropertiesLoader();
    Set urlSet = new HashSet();
    int sequence = 0;
    /**
     * It Reads and prints any RSS/Atom feed type. <p>
     *
     * @author Alejandro Abdelnur
     *
     */
    public void download() throws Exception {
        Collection<String> sourceColl = this.getSources();
        for (String source : sourceColl) {
            log.info("Try to download Rss" + source );
            this.downloadRss(source);
        }

    }

    private void downloadRss(String source) {
        boolean ok = false;
        try {
            
            //URL feedUrl = new URL(args[0]);
            URL feedUrl = new URL(source);

            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(feedUrl));
            
            // System.out.println(feed);
            ok = true;
            StringBuilder documentData;
            for (SyndEntry entry : (List<SyndEntry>) feed.getEntries()) {
                documentData = new StringBuilder();
                String title = entry.getTitle();
                documentData.append(title);
                documentData.append(".");
                documentData.append(" ");
                String uri = entry.getUri();
                String date = entry.getPublishedDate().toLocaleString();
                // Get the Links
                for (SyndLinkImpl link : (List<SyndLinkImpl>) entry.getLinks()) {
                    System.out.println("Link: " + link.getHref());
                }             
 
                // Get the Contents
                for (SyndContentImpl content : (List<SyndContentImpl>) entry.getContents()) {
                    System.out.println("Content: " + content.getValue());
                    documentData.append(content.getValue());
                }
                
                // Get the Categories
                for (SyndCategoryImpl category : (List<SyndCategoryImpl>) entry.getCategories()) {
                    System.out.println("Category: " + category.getName());
                }
                this.writeFile(documentData.toString());
                System.out.print("Title:" + title + "\n" + "uri" + uri + "Date:" + date);
                feed.createWireFeed();

            }

        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("ERROR: " + ex.getMessage());
        }


        if (!ok) {
            System.out.println();
            System.out.println("FeedReader reads and prints any RSS/Atom feed type.");
            System.out.println("The first parameter must be the URL of the feed to read.");
            System.out.println();
        }
    }
    
    private void writeFile( String pageAsText ){ 
        FacadePersistence facadePersistence = FacadePersistence.getInstance();
        facadePersistence.createOriginalFile(pageAsText, sequence++);
}

    private Collection<String> getSources() {
        String propertyFonts = properties.getProperty("fonts.rss");
        String[] fontsColl = propertyFonts.split(",");
        return Arrays.asList(fontsColl);
    }
}
